package org.pjsip.manager;

import android.util.Log;

import com.blankj.utilcode.util.StringUtils;

import org.pjsip.MyLogWriter;
import org.pjsip.bean.SeatInfo;
import org.pjsip.pjsua2.LogConfig;
import org.pjsip.pjsua2.MySipPhoneEvent;
import org.pjsip.pjsua2.Phone;
import org.pjsip.pjsua2.PhoneConfig;
import org.pjsip.pjsua2.PhoneEvent;
import org.pjsip.utils.LocalStorageUtils;

/**
 * author : GWFan
 * time   : 1/20/21 2:34 PM
 * desc   :
 */

public class SipManager {
    private static final String TAG = "SipManager";
    private Phone phone;
    private PhoneConfig phoneConfig;
    private MyLogWriter logWriter;
    private MySipPhoneEvent mySipPhoneEvent;
    private LogConfig logConfig;
    private static volatile SipManager instance;

    public static SipManager getInstance() {
        if (instance == null) {
            synchronized (SipManager.class) {
                if (instance == null) {
                    instance = new SipManager();
                }
            }
        }
        return instance;
    }

    /**
     * 注册sip
     */
    public void registerSip(String userName, String pwd) {
        unregisterSip();
        if (logWriter == null)
            logWriter = new MyLogWriter();

        if (mySipPhoneEvent == null) {
            mySipPhoneEvent = new MySipPhoneEvent();
        }
        if (logConfig == null) {
            logConfig = new LogConfig();
            int LOG_LEVEL = 4;
            logConfig.setLevel(LOG_LEVEL);
            logConfig.setConsoleLevel(LOG_LEVEL);
            logConfig.setWriter(logWriter);
        }
        if (phoneConfig == null) {
            phoneConfig = new PhoneConfig();
            phoneConfig.setLogConfig(logConfig);

            //SeatInfo seatInfo = LocalStorageUtils.getSeatInfo();

                /****************
                 * //TODO  自己填写
                 * **********************/
                phoneConfig.setLl_api_server("");
                phoneConfig.setApi_key("");
                phoneConfig.setSdk_secret("");
                /**************************************/

                phoneConfig.setSeatId(userName);
                phoneConfig.setSeatPwd(pwd);
                phoneConfig.setPe(mySipPhoneEvent);
        }
        if (phone == null) {
            phone = new Phone();
        }
        phone.init(phoneConfig);
    }

    /**
     * 外呼
     *
     * @param cPhone 外呼号码
     */
    public void makeCall(String cPhone) {
        Log.d(TAG, "makeCall: --------<"+cPhone);
        if (StringUtils.isEmpty(cPhone)) {
            return;
        }
        if (phone != null) {
            Log.d(TAG, "makeCall: ------>"+cPhone);
            phone.callout(cPhone);
        }
    }

    /**
     * 挂断电话
     */
    public void dropCall() {
        if (phone != null) {
            phone.dropCall();
        }
    }

    /**
     * 注销SIP
     */
    public void unregisterSip() {
        if (phone != null) {
            phone.unInit();
            phone.delete();
            phoneConfig = null;
            logConfig = null;
            logWriter = null;
            phone = null;
        }
    }

    /**
     * 登陆分机
     */
    public void login() {
        if (phone != null) {
            phone.login();
        }
    }

    /**
     * ResetRetryCount
     */
    public void resetLoginCnt() {
        if (phone != null) {
            phone.resetLoginCnt();
        }
    }
}

